pkgname=nvml
pkgver=git
pkgrel=1
url='https://github.com/pmem/nvml.git'
arch=('i386' 'x86_64')
license=('Intel' 'GPL' 'BSD')
makedepends_debian_11=(cmake clang pkg-config udev libkmod-dev libudev-dev uuid-dev libjson-c-dev bash-completion libkeyutils-dev libiniparser-dev)
source=('https://github.com/pmem/valgrind.git'
	'https://github.com/llvm-mirror/llvm.git'
	'https://github.com/llvm-mirror/libcxxabi.git'
	'https://github.com/pmem/libcxx.git'
	'https://github.com/pmem/ndctl.git'
	'https://github.com/ofiwg/libfabric.git'
	'https://github.com/pmem/nvml.git')
md5sums=('SKIP' 'SKIP' 'SKIP' 'SKIP' 'SKIP' 'SKIP' 'SKIP')

build_install_valgrind_pmem()
{
	cd "${srcdir}/valgrind"
	git submodule init
	git submodule update
	./autogen.sh
	./configure --prefix=/usr/local CFLAGS="-fno-stack-protector"
	make
	make install
}

build_install_libfabric()
{
	cd "${srcdir}/libfabric"
	./autogen.sh
	./configure --prefix=/usr/local --with-valgrind=/usr/local
	make
	make install
}

# The c++ subtest needs a custom version of the standard library -- libc++.
# please visit https://github.com/pmem/nvml/blob/master/utils/docker/images/install-libcxx.sh
# to get more detail info.
build_install_libcxx()
{
	cd "${srcdir}/libcxxabi"
	git checkout release_39

	cd "${srcdir}/llvm"
	git checkout release_39

	cd "${srcdir}/libcxx"
	git checkout release_39

	export CC=clang
	export CXX=clang++
	local install_path=/usr/local/libcxx

	CMAKE=cmake
	[ "$DISTRO" = "centos" -o "$DISTRO" = "aliyun" ] && CMAKE=cmake3

	cp -af "${srcdir}/libcxxabi" "${srcdir}/llvm/projects"
	cp -af "${srcdir}/libcxx" "${srcdir}/llvm/projects"

	mkdir -p "${srcdir}/build/abi"
	mkdir -p "${srcdir}/build/lib"

	cd "${srcdir}/build/abi"
	$CMAKE -DLLVM_PATH="${srcdir}/llvm" -DCMAKE_INSTALL_PREFIX="${install_path}" "${srcdir}/llvm/projects/libcxxabi/"
	make
	make install

	# xlocale.h was removed since libc6-de 2.26: https://sourceware.org/glibc/wiki/Release/2.26#Removal_of_.27xlocale.h.27
	# here touch a fake header to avoid compiling error.
	[ -f /usr/include/xlocale.h ] || touch "${srcdir}/llvm/projects/libcxx/include/xlocale.h"
	cd "${srcdir}/build/lib"
	$CMAKE -DLLVM_PATH="${srcdir}/llvm" -DLIBCXX_CXX_ABI=libcxxabi -DLIBCXX_CXX_ABI_INCLUDE_PATHS="${srcdir}/llvm/projects/libcxxabi/include" -DCMAKE_INSTALL_PREFIX="${install_path}" -DLIBCXX_CXX_ABI_LIBRARY_PATH="${install_path}/lib" "${srcdir}/llvm/projects/libcxx"
	make
	make install
}

# The dax tests need ndctl tool to create dax device.
# See more details at https://nvdimm.wiki.kernel.org.
build_install_ndctl()
{
	cd "${srcdir}/ndctl"
	[[ -e '/usr/include/iniparser.h' ]] || cp /usr/include/iniparser/* /usr/include/
	local distro=$(basename $rootfs)
	if [[ "$distro" =~ "debian-10" ]]; then
		git checkout v72
	fi
	./autogen.sh
	./configure CFLAGS='-g -O0' --disable-docs --enable-debug --prefix=/usr/local
	make
	make install
}

build()
{
	[ "$DISTRO" = "centos" ] && {
		cd "${srcdir}/nvml"
		sed -i 's/CXXFLAGS += -Wno-unknown-attributes/CXXFLAGS += -Wno-ignored-attributes/' ./src/benchmarks/Makefile
	}

	build_install_valgrind_pmem
	build_install_libfabric
	build_install_libcxx
	build_install_ndctl

}

package()
{
	benchmark_path="${pkgdir}/lkp/benchmarks"
	mkdir -p "${benchmark_path}"
	cp -af "${srcdir}/${pkgname}" "${benchmark_path}"
	. $LKP_SRC/lib/env.sh

	cd "${srcdir}/valgrind"
	make DESTDIR="${pkgdir}" install
	cd "${srcdir}/libfabric"
	make DESTDIR="${pkgdir}" install
	cp -af "/usr/local/libcxx" "${pkgdir}/usr/local"
	cd "${srcdir}/ndctl"
	make DESTDIR="${pkgdir}" install
}
